<?php

namespace App\Hospital\Controllers;

use App\Admin\Actions\DownLoadAction;
use App\Admin\Exporter\GuideExporter;
use App\Admin\Exporter\HealthControlExporter;
use App\Admin\Exporter\SignExporter;
use App\Admin\Forms\DataDownLoadForm;
use App\Admin\Forms\DataUploadForm;
use App\Admin\Forms\HospitalAppointStreetForm;
use App\Admin\Grids\HospitalUploadDataGrid;
use App\Admin\Grids\SignChildBigDataGrid;
use App\Data\SignData;
use App\Models\FamilyEdu;
use App\Models\HospitalAppointStreet;
use App\Models\HospitalImportBigchild;
use App\Models\HospitalImportChild;
use App\Models\HospitalImportPregn;
use App\Models\HospitalUserFamily;
use App\Models\ImportFamily;
use Dcat\Admin\Grid;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Card;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class DataController extends AdminController
{
    public function download(Request $request)
    {
        $input = $request->input();
        switch ($input['type']) {
            case 'bigChild':
                $fileName = "0-6岁儿童基本签约台账数据（".date('Ymd').").xlsx";
                $model = new SignExporter($input);
                break;
            case 'preg':
                $fileName = "孕产妇基本签约台账数据（".date('Ymd').").xlsx";
                $model = new SignExporter($input);
                break;
            case 'child':
                $fileName = "36月龄中医药儿童健康管理（".date('Ymd').").xlsx";
                $model = new HealthControlExporter($input);
                break;
            case 'guide':
                $fileName = "中医药儿童健康指导记录（".date('Ymd').").xlsx";
                $model = new GuideExporter($input);
                break;
        }
        return Excel::download($model,$fileName);
    }
    public function index(Content $content)
    {
        return $content
            ->header('数据管理')
            ->title('数据预览')
            ->description('数据已导出表格为准')
            ->body(function (Row $row) {
                $row->column(6,function (Column $column){
                    $card = new Card(new Card('36月龄中医药儿童健康管理',$this->Grid($this->childData())));
                    $card->noPadding();
                    $column->row($card);
                });
                $row->column(6,function (Column $column){
                    $card = new Card(new Card('中医药儿童健康指导记录',$this->Grid($this->guideData())));
                    $card->noPadding();
                    $column->row($card);
                });
                $row->column(6,function (Column $column){
                    $card = new Card(new Card('0-6岁儿童家医签约',$this->Grid($this->bigChildData())));
                    $card->noPadding();
                    $column->row($card);
                });
                $row->column(6,function (Column $column){
                    $card = new Card(new Card('孕产妇家医签约',$this->Grid($this->pregData())));
                    $card->noPadding();
                    $column->row($card);
                });

            });
    }
    public function childData()
    {
        $hospital_id = Admin::user()->hospital_id;
        $total = SignData::childTotle($hospital_id);
        $sign_total = SignData::childSignTotal($hospital_id);
        $rate = $total>0?round(($sign_total/$total)*100,2):0;
        $data = [ [
            'admin_total'=>$total,
            'sign_admin_total'=>$sign_total,
            'sign_rate'=>$total>0?$rate."%":"",
        ]];
        $lable = [
            'admin_total'=>"管辖总数",
            'sign_admin_total'=>"管辖签约数",
            'sign_rate'=>"管理率"
        ];
        $type = 'child';

        return [$data,$lable,$type];
    }
    public function bigChildData()
    {
        $hospital_id = Admin::user()->hospital_id;

        $admin_total = SignData::bigChildTotle($hospital_id);
        $sign_total = SignData::bigChildSignTotal($hospital_id);

        $rate =$admin_total>0?round(($sign_total/$admin_total)*100,2)."%":"";

        $data = [ [
            'admin_total'=>$admin_total,
            'sign_admin_total'=>$sign_total,
            'sign_rate'=>$rate
        ]];
        $lable = [
            'admin_total'=>"管辖总数",
            'sign_admin_total'=>"管辖签约数",
            'sign_rate'=>"管理率"
        ];
        $type = 'bigChild';
        return [$data,$lable,$type];
    }
    public function pregData()
    {
        $hospital_id = Admin::user()->hospital_id;

        $admin_total = SignData::pregTotle($hospital_id);
        $sign_total = SignData::pregSignTotal($hospital_id);

        $rate = $admin_total>0?round(($sign_total/$admin_total)*100,2)."%":"";

        $data = [ [
            'admin_total'=>$admin_total,
            'sign_admin_total'=>$sign_total,
            'sign_rate'=>$rate
        ]];
        $lable = [
            'admin_total'=>"管辖总数",
            'sign_admin_total'=>"管辖签约数",
            'sign_rate'=>"管理率"
        ];
        $type = 'preg';

        return [$data,$lable,$type];
    }
    public function guideData()
    {

        $day = FamilyEdu::query()
            ->where('hospital_id',Admin::user()->hospital_id)
            ->where('created_at',">=",date('Y-m-d',strtotime("-1 day")))
            ->groupBy('family_id','family_type_id')
            ->get()
            ->count();
        $month = FamilyEdu::query()
            ->where('hospital_id',Admin::user()->hospital_id)
            ->where('created_at',">=",date('Y-m-d',strtotime("-1 month")))
            ->groupBy('family_id','family_type_id')
            ->get()
            ->count();

        $total = FamilyEdu::query()
            ->where('hospital_id',Admin::user()->hospital_id)
            ->where('created_at',">=",'2017-01-01')
            ->groupBy('family_id','family_type_id')
            ->get()
            ->count();


        $data = [ [
            'day'=>$day,
            'month'=>$month,
            'total'=>$total
        ]];
        $lable = [
            'day'=>"昨日宣教数",
            'month'=>"近一月宣教总数",
            'total'=>"宣教总数",
        ];
        $type = 'guide';
        return [$data,$lable,$type];
    }

    public function Grid($data)
    {
        list($data,$lable,$type) = $data;
        return new Grid(null, function (Grid $grid) use($data,$lable,$type){
            if($lable) {
                foreach ($lable as $k => $v) {
                    $grid->column($k, $v);
                }
            }
            $grid->model()->setData($data);
            $grid->showCreateButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->disableDeleteButton();

            $grid->disableFilter();
            $grid->disableCreateButton();
            $grid->disableActions();

            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->disablePagination();
            $grid->disableToolbar();


            $grid->column('action','操作')->display('导出台账')->modal(function($model) use ($type){

                return DataDownLoadForm::make()->payload(['type'=>$type,'hospital_id'=>Admin::user()->hospital_id]);
            });

            //$grid->disableToolbar();
        });
    }
}
